home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12937 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: 3 Apr 1996 08:09:18 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ju7reINN3bd@keats.ugrad.cs.ubc.ca>
  8. References: <31616F63.481D@lava.weeg.uiowa.edu>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <31616F63.481D@lava.weeg.uiowa.edu>,
  12. Artur Wojdat  <awojdat@lava.weeg.uiowa.edu> wrote:
  13. >Hello everybody,
  14. >    Is there a function or some sort of way that I could remove '\n' 
  15. >charecter form the end of the string. I'm reading from two files, want to 
  16. >form one line of text and then have it printed out to stdout. I use fgets to 
  17. >read from the file and I noticed that it appends newline char at the end.
  18. >    It is important that two lines of text, one from each file, will be 
  19. >combined into one and I can't do it because the first string has '\n' added 
  20. >to it. I'm picky becauuse the output will be used to feed another program and 
  21. >I'm affraid that not properly formatted input may corrupt the process.
  22. >    Any suggestions will be greatly appreciated .. Thanks, Art ...
  23.  
  24. For this sort of problem, a character-oriented approach is perhaps best. What
  25. you are asking for can be solved by a regular expression automaton. It would be
  26. foolish to read whole lines into buffers and process that way, since this
  27. problem does not call for more than one character of lookahead! In fact, it
  28. calls for no lookahead at all! Only a fool would try to read lines from the
  29. standard IO library into yet another buffer and then do string operations.
  30.  
  31. Just read a character from one file using c = getc(stream) and use putchar(c)
  32. to put it onto the standard output unless c is EOF or '\n', in which case you
  33. switch to the second file and do the same thing. You repeat until both files
  34. reach EOF. No silly-willy string buffering and concatenation needed. In any
  35. case, fgets() calls for a fixed-size buffer, so your program would not be
  36. robust for files with long lines unless you took extra care that would further
  37. complicate your program needlessly.
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. -- 
  50.  
  51.